package com.sec.android.app.myfiles.module.shortcut.cmd;

import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.widget.Toast;

import com.sec.android.app.myfiles.R;
import com.sec.android.app.myfiles.dialog.AddShortcutDialogFragment;
import com.sec.android.app.myfiles.fragment.AbsMyFilesFragment;
import com.sec.android.app.myfiles.info.AppConstants;
import com.sec.android.app.myfiles.info.AppFeatures;
import com.sec.android.app.myfiles.log.Log;
import com.sec.android.app.myfiles.log.SamsungAnalyticsLog;
import com.sec.android.app.myfiles.module.abstraction.FileRecord;
import com.sec.android.app.myfiles.module.local.foldertree.FolderTreeDbTableInfo;
import com.sec.android.app.myfiles.module.shortcut.ShortcutUtils;
import com.sec.android.app.myfiles.provider.DbTableInfo;
import com.sec.android.app.myfiles.util.PreferenceUtils;

import org.puremvc.java.multicore.interfaces.INotification;
import org.puremvc.java.multicore.patterns.command.SimpleCommand;

import java.util.List;

public class AddShortcutCmd extends SimpleCommand implements AddShortcutDialogFragment.ChooseCallback {

    private Context mContext;
    private AbsMyFilesFragment mFragment;
    private List<FileRecord> mRecordList;
    private FileRecord mSrc = null;

    @Override
    public void execute(INotification notification) {
        Object[] params = (Object[]) notification.getBody();
        mContext = (Context) params[0];
        mFragment = (AbsMyFilesFragment) params[1];
        mRecordList = (List<FileRecord>) params[2];

        if (mFragment != null) {
            if (mRecordList != null && !mRecordList.isEmpty()) {
                mSrc = mRecordList.get(0);
            }
            AddShortcutDialogFragment addShortcutDialog = AddShortcutDialogFragment.getInstance(this);
            addShortcutDialog.setTargetFragment(mFragment, R.id.menu_add_shortcut);
            addShortcutDialog.show(mFragment.getFragmentManager(), "add_shortcut");
        } else {
            Log.e(this, "execute fail - fragment is null");
        }
    }

    @Override
    public void onClick(FileRecord.ShortcutType type) {
        if (mSrc != null) {
            if (type == FileRecord.ShortcutType.MyFiles) {
                int shortcutsCount = ShortcutUtils.getShortcutItemsCount(mContext);
                if (shortcutsCount >= AppConstants.MAX_SHORTCUTS_COUNT) {
                    Toast.makeText(mContext, R.string.maximum_number_of_shortcuts, Toast.LENGTH_SHORT).show();
                } else {
                    SamsungAnalyticsLog.sendLog(mFragment.getProcessId(), SamsungAnalyticsLog.Event.SELECT_SHORTCUT_LOCATION, "Main screen", SamsungAnalyticsLog.SelectMode.SELECTION_MODE);
                    addShortcutToMyFilesDB(mContext, mSrc, type);
                }
            } else if (type == FileRecord.ShortcutType.HomeScreen) {
                SamsungAnalyticsLog.sendLog(mFragment.getProcessId(), SamsungAnalyticsLog.Event.SELECT_SHORTCUT_LOCATION, "Home screen", SamsungAnalyticsLog.SelectMode.SELECTION_MODE);
                addShortcutToHome();
                addShortcutToMyFilesDB(mContext, mSrc, type);
            }
            if (AppFeatures.isTabletUIMode()) {
                DbTableInfo tableInfo = FolderTreeDbTableInfo.getInstance();
                mContext.getContentResolver().notifyChange(Uri.parse(tableInfo.getUri()), null);
            }
        } else {
            Log.e(this, "Current file is null, can't add shortcut on " + (type == FileRecord.ShortcutType.HomeScreen ? "Home Screen" : "MyFiles home screen"));
        }
        mFragment.finishActionMode();
    }

    private void addShortcutToHome() {
        Intent addShortcutIntent = ShortcutUtils.getIntentForShortcut(mContext, mSrc);
        mContext.sendBroadcast(addShortcutIntent);
    }

    public static void addShortcutToMyFilesDB(Context context, FileRecord record, FileRecord.ShortcutType type) {

        String path = record.getFullPath();
        ContentResolver cr = context.getContentResolver();
        DbTableInfo tableInfo = DbTableInfo.getInstance(FileRecord.StorageType.Shortcut);
        boolean isAdded = false;
        if (!ShortcutUtils.isAddedToShortcut(context, record, type) && tableInfo != null) {
            ContentValues values = new ContentValues();
            values.put(tableInfo.getColumnName(DbTableInfo.COLUMN_ID.SHORTCUT_TYPE), type.ordinal());
            values.put(tableInfo.getColumnName(DbTableInfo.COLUMN_ID.PATH), path);
            values.put(tableInfo.getColumnName(DbTableInfo.COLUMN_ID.NAME), record.getNameWithoutExt());
            values.put(tableInfo.getColumnName(DbTableInfo.COLUMN_ID.FILE_TYPE), record.getFileType());
            values.put(tableInfo.getColumnName(DbTableInfo.COLUMN_ID.MIME_TYPE), record.getMimeType(context));
            Uri uri = Uri.parse(tableInfo.getUri());
            cr.insert(uri, values);
            isAdded = true;
        }
        if (type == FileRecord.ShortcutType.MyFiles) {
            String addShortcutToast;
            if (isAdded) {
                addShortcutToast = String.format(context.getResources().getString(R.string.shortcut_added), record.getName());
                PreferenceUtils.setMyFilesShortcutCount(context, 1);
            } else {
                addShortcutToast = String.format(context.getResources().getString(R.string.shortcut_exists), record.getName());
            }

            Toast.makeText(context, addShortcutToast, Toast.LENGTH_SHORT).show();
        }
    }
}
